<?php declare(strict_types=1);

/**
 * @package MaterialDesignIcons (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2023-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 17.10.25
 */

namespace LightPortal\Plugins\MaterialDesignIcons;

use Bugo\Compat\Theme;
use LightPortal\Enums\PluginType;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\Plugins\Plugin;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute(type: PluginType::ICONS)]
class MaterialDesignIcons extends Plugin
{
	private string $prefix = 'mdi mdi-';

	public function init(): void
	{
		Theme::loadCSSFile(
			'https://cdn.jsdelivr.net/npm/@mdi/font@7/css/materialdesignicons.min.css',
			[
				'external' => true,
				'seed'     => false,
			]
		);
	}

	public function prepareIconList(Event $e): void
	{
		$mdIcons = $this->cache()->remember('all_md_icons', function () {
			$content = file_get_contents('https://raw.githubusercontent.com/Templarian/MaterialDesign/master/meta.json');
			$json = json_decode($content);

			$icons = [];
			foreach ($json as $icon) {
				$icons[] = $this->prefix . $icon->name;
			}

			return $icons;
		}, 30 * 24 * 60 * 60);

		$e->args->icons = array_merge($e->args->icons, $mdIcons);
	}

	public function credits(Event $e): void
	{
		$e->args->links[] = [
			'title' => 'Material Design Icons',
			'link' => 'https://pictogrammers.com/library/mdi/',
			'author' => 'Pictogrammers',
			'license' => [
				'name' => 'Pictogrammers Free License',
				'link' => 'https://pictogrammers.com/docs/general/license/'
			]
		];
	}
}
